use clap::Parser;
use tracing::info;
use tracing_subscriber::{fmt, filter::EnvFilter};

use yam::{
    cli::{Cli, Commands},
    config::Config,
    commands::{pull_model, run_model, list_processes},
    security::initialize_security,
    plugins::PluginManager,
    error::YamError
};

#[tokio::main]
async fn main() -> Result<(), YamError> {
    // 初始化系统组件
    let config = Config::load()?;
    initialize_logging(&config);
    initialize_security()?;
    
    let mut plugin_manager = PluginManager::new();
    plugin_manager.load_default_plugins()?;

    // 处理命令行参数
    let cli = Cli::parse();
    process_command(cli, config, plugin_manager).await?;

    Ok(())
}

async fn process_command(
    cli: Cli,
    config: Config,
    plugins: PluginManager
) -> Result<(), YamError> {
    match cli.command {
        Commands::Pull { model } => {
            pull_model(&model, &config).await
        }
        Commands::Run { model, args } => {
            run_model(&model, args, &config, plugins).await
        }
        Commands::Ps => {
            list_processes()
        }
        // 其他命令处理...
    }
}

fn initialize_logging(config: &Config) {
    let filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(config.log_level.as_str()));
    
    fmt()
        .with_env_filter(filter)
        .with_ansi(config.color_output)
        .init();
}